<%@page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="com.qidian.common.beans.UserBean"%>
<%@page import="com.qidian.util.*"%>
<%@page import="com.qidian.server.Status" %>
<%
	PageList pageList = (PageList)request.getAttribute("pageList");
	List<UserBean> listUser = (List<UserBean>)pageList.getList();

%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>开发者管理页面</title>
	<meta http-equiv="expires" content="0">    
	<%@ include file="../../common/extjs.jsp" %>
	<script>
var win;
Ext.onReady(function(){

	var tb = Ext.create('Ext.toolbar.Toolbar');
	tb.render('toolbar');
	tb.add({text: '创建',iconCls: 'add',handler: onCreateClick});
	tb.add({text: '更新',iconCls: 'update',handler: onUpdateClick});
	tb.add({text: '删除',iconCls: 'delete',handler: onDeleteClick});
	tb.add({text: '刷新',iconCls: 'refresh',handler: onRefreshClick});
	tb.doLayout();

	if(!win){
        win =  Ext.create('Ext.window.Window', {
        	contentEl: 'dlg',
        	title:'创建用户',
        	width:320,
        	height:400,
        	autoHeight:true,
        	buttonAlign:'center',
        	closeAction:'hide',
	        buttons: [
	        	{text:'提交',handler: function(){
	        			valiDataForm();	        			
	    	    	}
	        	},
	        	{text:'取消',handler: function(){win.hide();}}
	        ]
        });
        win.hide();
    }
});	
function onCreateClick(){
    document.getElementById('dataForm').reset();
    document.getElementById('user_id').value= '';
	document.getElementById('user_name').disabled=false;
    document.getElementById('user_name_empty').style.display = 'none';
    document.getElementById('user_name_exist').style.display = 'none';
   	document.getElementById('password_empty').style.display = 'none';
	document.getElementById('password_confirm_empty').style.display = 'none';
	document.getElementById('password_confirm_mismatch').style.display = 'none';

	closeMessageDiv() ;
    win.setTitle('创建');
    win.show();
}

function onUpdateClick(){
	document.getElementById('dataForm').reset();
	var checks = document.getElementsByName('check');
	var user_id = '';
	var check_count = 0 ;
	if(checks.length)
	{
		for(var i=0;i<checks.length;i++){
			if(checks[i].checked){
				check_count++;
				if(user_id == ''){
					user_id = checks[i].value;
				}else {
					Ext.MessageBox.alert('警告','选择一个用户');
					return false;
				}
			}
		}
		if(check_count == 0){
			Ext.MessageBox.alert('警告','选择一个用户');
			return false;
		}
	}else {
		Ext.MessageBox.alert('警告','选择一个用户');
		return false;
	}
	document.getElementById('user_id').value = user_id;

	
	Ext.Ajax.request(
	{
		url: 'admin',
		method: 'POST',
		params: {
	        'actionType': 'beforeUpdate',
	        'user_id': user_id
	    },
		success: function(transport) 
		{
			var data = eval('('+transport.responseText+')');
			document.getElementById('user_id').value = data.item.user__id;
	       	document.getElementById('user_name').value = data.item.user__name;
	       	document.getElementById('password').value = data.item.password;
	       	document.getElementById('password_confirm').value=data.item.password;
			document.getElementById('user_name').disabled=true;
		}
	}
	);
	
	closeMessageDiv() ;
	win.setTitle('更新');
    win.show();
   }

function onDeleteClick(){
	var checks = document.getElementsByName('check');
	var user_id = '';
	var check_count = 0;
	if(checks.length){
		for(var i=0;i<checks.length;i++){
			if(checks[i].checked){
				check_count = check_count + 1;
				if(user_id == ''){
					user_id = checks[i].value;
				}else {
					user_id = user_id + ',' + checks[i].value;
				}
			}
		}
		if(check_count == 0){
			Ext.MessageBox.alert('警告','选择一个用户');
			return false;
		}
	}else {
		Ext.MessageBox.alert('警告','选择一个用户');
		return false;
	}
	
	Ext.MessageBox.confirm('确认删除', '确认删除', function (btn){
		if(btn=="yes"){
			document.getElementById('user_id').value = user_id;
			document.getElementById('listForm').action = 'admin?actionType=deleteUser';
			document.getElementById('listForm').submit();
		}
	});
}

function onRefreshClick(){
	window.location.href = 'admin?actionType=userAdmin';
}

function valiDataForm()
{
	var user_id = document.getElementById('user_id').value ;
	var user_name = document.getElementById('user_name').value.replace(/(^\s*)|(\s*$)/g, "") ;
	var password = document.getElementById('password').value ;
	var password_confirm = document.getElementById('password_confirm').value ;

	var success = true;
	
	if(user_name == '')
	{
		document.getElementById('user_name_empty').style.display = '';
		success = false;
	}
	else
		document.getElementById('user_name_empty').style.display = 'none';
	
	if(!valiUserName(user_name))
	{
		document.getElementById('user_name_invalid').style.display = '';
		success = false;
	}
	else 
		document.getElementById('user_name_invalid').style.display = 'none';
	
	if (!valiPassword(password))
	{
		document.getElementById('password_invalid').style.display='';
		success = false ;
	}
	else
		document.getElementById('password_invalid').style.display='none';
			
	if (password != password_confirm)
	{	document.getElementById('password_confirm_mismatch').style.display='';
		success = false ;
	}
	else
		document.getElementById('password_confirm_mismatch').style.display='none';
	
	
	if(success)
	{
		// If user_id is not set, send create request
		if (user_id == '')
		{
			Ext.Ajax.request({
				url: 'admin',
				method: 'GET',
				params: {
			        'action' : 'createUser',
			        'user_name' : user_name,
			        'password': password
			    },
				success: function(transport) {
				    var data = eval('('+transport.responseText+')');
				    if(data.statusCode == '<%=Status.USER_EXISTS.getStatusCode()%>'){//user name exists already
				    	document.getElementById('user_name_exist').style.display = '';
				    	document.getElementById('unknown_error').style.display = 'none';
				    	return false;
				    }else if(data.statusCode == '<%=Status.OTHER_ERROR.getStatusCode()%>'){//unknown error
				    	document.getElementById('user_name_exist').style.display = 'none';
				    	document.getElementById('unknown_error').style.display = '';
				    	return false;
				    }else if(data.statusCode == '<%=Status.OK.getStatusCode()%>'){//create successful
				    	document.getElementById('user_name_exist').style.display = 'none';
				    	document.getElementById('unknown_error').style.display = 'none';
				 		win.close() ;
				 		window.location.href = 'admin?action=userAdmin';
				    }
		  		}
			});
		}
		else // If user_id is  set, send upate request
			Ext.Ajax.request({
				url: 'admin',
				method: 'POST',
				params: {
			        'action' : 'updateUser',
			        'user_id': user_id,
			        'user_name' : user_name,
			        'newPassword': password,
			        'oldPassword': oldPassword
			    },
				success: function(transport) 
				{
				    var data = eval('('+transport.responseText+')');
				    if(data.statusCode == '<%=Status.OK.getStatusCode()%>')
				    {//update successful
						document.getElementById('unknown_error').style.display = 'none';
				 		win.close() ;
				 		window.location.href = 'admin?acitonType=userAdmin';
				    }else{
				    	document.getElementById('unknown_error').style.display = '';
				    	return false;
				    }
				}
			});
	}
}

function valiInteger(cyclenum){
	var re = /^[1-9]\d*$/;
    if (!re.test(cyclenum)){
        return false;
    }else {
    	return true;
    }
}

function valiUserName(user_name){
	var re = /^[0-9a-zA-Z_]{5,}$/;
	if (re.test(user_name))
		return true ;
	else
		return false;
}

function valiPassword(password){
	var re = /^[0-9a-zA-Z_]{5,16}$/;
	if (re.test(password))
		return true ;
	else
		return false ;
}

function valiEmail(email){
	if(email.length > 32)
		return false;
	var filter = /^[a-zA-Z_0-9\.\-]+@([a-zA-Z_0-9]+\.)+[a-zA-Z]{2,3}$/;
	if (filter.test(email))
		return true ;
	else
		return false ;
}

function checkAll(){
	var checkall = document.getElementById('checkall');
	var checks = document.getElementsByName('check');
	if(checkall.checked == 'true'){
		for(var i=0;i<checks.length;i++){
			if (checks[i].disabled == true)
				continue ;
			checks[i].checked = 'true';
		}
	}else {
		for(var i=0;i<checks.length;i++){
			if (checks[i].disabled == true)
				continue ;
			checks[i].checked = !checks[i].checked;
		}
	}
}

function closeMessageDiv()
{
	var divs = document.getElementsByTagName('div') ;
	for (var i=0;i < divs.length ;i ++)
	{
		var divType = divs[i].getAttribute('divType') ;
		if ( divType && divType == 'message')
			divs[i].style.display = 'none'  ;
	}
}

</script>
</head>
<body>
	<div id="toolbar"></div>	
	<form id="listForm" name="listForm" action="" method="get">
	<%=pageList.getPageInfo().getHtml("admin?action=userAdmin") %>
	<br />
	<input type="hidden" name="user_id" id="user_id">
	<table width="90%" align="center" id="the-table">
		<tr align="center" bgcolor="#ADD8E6" class="b_tr">
			<td><input type="checkbox" name="checkall" id="checkall" onclick="checkAll();">选择</td>
			<td>用户ID</td>
			<td>用户名</td>
			<td>可用点数</td>
			<td>操作</td>
		</tr>
<%
	for(UserBean userBean:listUser)
	{
%>	
	<tr align="center">
		<td align="center" nowrap="nowrap"><input type="checkbox" name="check" value="<%=userBean.getUserId() %>" class="ainput"></td>
		<td><%=userBean.getUserId() %></td>
		<td><%=userBean.getUserName() %></td>
		<td><%=userBean.getLeftPoint() %></td>
		<td><button onClick="onDelete(<%=userBean.getUserId() %>)">删除</button></td>
	</tr>
<%
	}
%>		
	</table>
	</form>
	<div id="dlg" class="x-hidden">
		<form id="dataForm" name="dataForm" action="" method="get">
			<input type="reset" style="display: none;">
			<input type="submit" style="display: none;">
			<input type="hidden" name="user_id">
			<table border="0" align="center" height="30" width="98%">
				<tr height="30">
					<td width="100">用户名</td>
					<td>
						<input type="text" id="user_name" name="user_name" style="width: 195px" value="" maxlength="50">
						<div id="user_name_empty" style="display:none;" divType="message"><font color="red">用户名为空</font></div>
						<div id="user_name_exist" style="display:none;" divType="message"><font color="red">用户名已存在</font></div>
						<div id="user_name_invalid" style="display:none;" divType="message"><font color="red">无效的用户名</font></div>
					</td>
				</tr>
				<tr height="30">
					<td width="100">密码</td>
					<td>
						<input type="password" id="password" name="password" style="width: 195px" maxlength="50">
						<div id="password_empty" style="display:none;" divType="message"><font color="red">密码为空</font></div>
						<div id="password_invalid" style="display:none;" divType="message"><font color="red">无效的密码</font></div>
					</td>
				</tr>
				<tr height="30">
					<td width="100">确认密码</td>
					<td>
						<input type="password" id="password_confirm" name="password_confirm" style="width: 195px" maxlength="50">
						<div id="password_confirm_empty" style="display:none;" divType="message"><font color="red">确认密码为空</font></div>
						<div id="password_confirm_mismatch" style="display:none;" divType="message"><font color="red">密码不匹配</font></div>
					</td>
				</tr>
			</table>
		</form>
	</div>
</body>
</html>